package cn.j0n4than.accountingtool.mapper;

import cn.j0n4than.accountingtool.entity.Spending;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * SpendingMapper
 *
 * @author jonathan
 */
@Mapper
public interface SpendingMapper extends BaseMapper<Spending> {
    /**
     * 获取最近的数据
     *
     * @param userId 用户ID
     * @param limit  数据条数
     * @return List
     */
    @Select("" +
            "SELECT spending.*, type.name as type, type.style as style, type.icon as icon  FROM ${prefix}spending spending " +
            "LEFT JOIN ${prefix}types type " +
            "ON spending.type_id = type.id " +
            "WHERE spending.user_id = #{uid}  ORDER BY spending.spent_at DESC LIMIT #{limit}" +
            "")
    List<Spending> getTopDataWithType(@Param("uid") Integer userId, @Param("limit") Integer limit);

    /**
     * 获取带Type的数据
     * 我不想写xml
     *
     * @param queryWrapper 查询构造
     * @param page         分页构造
     * @return List
     */
    @Select("<script>" +
            "SELECT spending.*, type.name as type, type.style as style, type.icon as icon  FROM ${prefix}spending spending " +
            "LEFT JOIN ${prefix}types type " +
            "ON spending.type_id = type.id " +
            "<where>${ew.sqlSegment}</where>" +
            "</script>")
    Page<Spending> getDataWithType(@Param("ew") QueryWrapper<Spending> queryWrapper, Page<Spending> page);
}
